package com.wendy.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;

import javax.crypto.Cipher;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;

/**
 * Description: 根据MAC地址生成秘钥
 * date: 2022/6/9 18:12
 *
 * @author Wendy
 * @since JDK 1.8
 */
public class LicenseUtil {

  public static final String ALGORITHM = "RSA";
  public static final String MODULUS = "19096587034012647821701400476266217921295608807551276300497701332651001793811978817897758326973103952453396739468169000863891467379125363906650019303645533313798936733425180642740934467201083404654598155148246165272100219967472307496966407528282360268729221369935099119823495500431074079412010616109129139052136134828252007859628879252332869240733451304555299591616021052102352954880987331657401963503384124455463963963972536992356502999186777372153534499297884642832354247599203994048464077644904459239193682144647924152467662162450951976536520841216450620940069974328463301262394549082155756103479551812557419499363";
  public static final String PUBLIC_EXPONENT = "65537";
  // 私钥 解密用
  //public static final String PRIVATE_EXPONENT = "5960307731216392437751226738209339572761976870446638642700771789361372075200635163649520829123012059560770714341843491656177425961518063364367092556205950591784141033038010132402548400546228253386786170614422010629732975257253857360734361749714141314018893497139363298533494063831387159839063081198593718652145459404129412807800628724573291847464480301893885142066373591449697312114311834311647512920475506145376424614274462532582072340661346685604438858952610275788909429602643921200478446891461712996358722050676031892958889624284895599512037721886444304511409056761195988929730000467246263965728015459747690620473";

  public static void main(String[] args) throws Exception {
    // 时间(天) 0.003约4分钟
    // 本机
    //String mac = Base64.encode("14-18-C3-BB-3F-DC") + ",0.003";
    // 大庆 10.0.6.21
    String mac = Base64.encode("08:94:ef:b1:ba:b7") + ",0.3";

    System.out.println(handleMacAndGenerate(mac));
  }

  public static String handleMacAndGenerate(String mac) throws Exception {
    if (StrUtil.isEmpty(mac)) {
      return "";
    }
    String[] sub = mac.split(",");
    String license;
    if (sub.length > 1) {
      // Base64解码一下mac
      license = Base64.decodeStr(sub[0]) + "," + (System.currentTimeMillis() + Math.round(Double.parseDouble(sub[1]) * 86400000));
    } else {
      license = Base64.decodeStr(mac);
    }
    return generatorLicense(license);
  }

  /**
   * @param license mac+time文本
   * @return 秘钥
   * @throws Exception Exception
   */
  public static String generatorLicense(String license) throws Exception {
    KeyFactory factory = KeyFactory.getInstance(ALGORITHM);
    RSAPublicKeySpec publicSpec = new RSAPublicKeySpec(new BigInteger(MODULUS), new BigInteger(PUBLIC_EXPONENT));
    return encrypt(license.getBytes(Charset.defaultCharset()), factory.generatePublic(publicSpec));
  }

  /**
   * 公钥加密
   *
   * @param content   文本
   * @param publicKey 公钥
   * @return 秘钥
   * @throws Exception Exception
   */
  private static String encrypt(byte[] content, PublicKey publicKey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] bytes = cipher.doFinal(content);
    return Base64.encode(bytes);
  }
}
